/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.core;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.jxreport.core.views.ViewObject;
import org.jxreport.core.views.br.BrView;
import org.jxreport.core.views.chart.ChartView;
import org.jxreport.html.HTMLBrView;
import org.jxreport.html.HTMLDivView;
import org.jxreport.html.HTMLView;
import org.jxreport.html.HTMLTableView;
import org.jxreport.html.HTMLTextContentView;
import org.jxreport.html.HTMLTwoColsView;
import org.jxreport.html.chart.HTMLBarChartView;
import org.jxreport.html.chart.HTMLLineChartView;
import org.jxreport.html.chart.HTMLPieChartView;
import org.w3c.dom.Element;

public class HTMLUtils {
	
	private static Random RANDOM_GENERATOR = new Random(GregorianCalendar.getInstance().getTimeInMillis());
	
	public static int getRandomInt(){
		int num = HTMLUtils.RANDOM_GENERATOR.nextInt();
		if(num < 0) num *= -1;
		
		return num;
	}
	
	public static String getDIVCode(HTMLView htmlReportView)
	{
		if(htmlReportView instanceof HTMLBrView)
			return "<br " + getStyleCode(htmlReportView) + " />";
		
		ViewObject rv = (ViewObject)htmlReportView;
		//HTMLReportView htmlReportView = (HTMLReportView)rv;
		
		String html = "<div id='" + rv.getId() + "'>";
		//if(rv.getTitle() != null) html += "<" + rv.getTitle().getType() + " " + HTMLUtils.getStyleCode(htmlReportView) + ">" + rv.getTitle().getContent() + "</" + rv.getTitle().getType() + ">";
		if(! (htmlReportView instanceof ChartView))
			if(rv.getTitle() != null) html += "<h2>" + rv.getTitle() + "</h2>";
		
		html += htmlReportView.getInnerDivCode();

		html += "</div>";
		return html;
	}
	
	public static String getCSSCode(HTMLView htmlReportView)
	{
		ViewObject rv = (ViewObject)htmlReportView;
		
		if(rv.getCssProperties().keySet().size() == 0) return "";
		
		String css = "#" + rv.getId() + "{ ";
		
		for (String cssProperty : rv.getCssProperties().keySet())
			css += cssProperty + ": " + rv.getCssProperties().get(cssProperty) + "; ";
		
		css += "} ";
		return css;
	}
	
	public static String getStyleCode(HTMLView htmlReportView)
	{
		ViewObject rv = (ViewObject)htmlReportView;
		String css = "style='";
		
		for (String cssProperty : rv.getCssProperties().keySet())
			css += cssProperty + ": " + rv.getCssProperties().get(cssProperty) + "; ";
		
		css += "' ";
		return css;
	}

	public static final String getJSScriptCode() { return fileListToString(getJSFilePathList()); }
	public static final String getCSScriptCode() { return fileListToString(getCSSFilePathList()); }
	
	private static final String fileListToString(List<String> filePathList)
	{
		try{
			String stringCode = "";
			for (String filePath : filePathList) {
		
				StringBuffer fileData = new StringBuffer(1000);
				//BufferedReader reader = new BufferedReader(new FileReader(filePath));
				//System.out.println("libs/" + filePath);
				InputStreamReader iReader = new InputStreamReader(HTMLUtils.class.getResourceAsStream("libs/" + filePath));
				
				BufferedReader reader = new BufferedReader(iReader);
				//InputStream inputStream = getClass().getResourceAsStream("schema.xsd");
				
				char[] buf = new char[1024];
				int numRead=0;
				while((numRead=reader.read(buf)) != -1){
				    String readData = String.valueOf(buf, 0, numRead);
				    fileData.append(readData);
				    buf = new char[1024];
				}
				reader.close();
				//String a = fileData.toString() + "/* lalala */";
				//js += a.replaceAll("\\/\\*.*?\\*\\/", "");
				stringCode += fileData.toString().replaceAll("(?s)\\s*/\\*.*?\\*/", "");//"\\/\\*.*?\\*\\/", "");
				//js += fileData.toString();
			}
			
			return stringCode;
		} catch(Exception e){e.printStackTrace(); return ""; }
	}
	
	public static final LinkedList<String> getJSFilePathList(){
		LinkedList<String> jsFilePathList = new LinkedList<String>();
		
		// Adding jQuery
		jsFilePathList.add("jqPlot/jquery.js");
		
		// Adding DataTables
		jsFilePathList.add("DataTables-1.9.3/media/js/jquery.dataTables.min.js");
		
		// Adding jqPlot
		jsFilePathList.add("jqPlot/jquery.jqplot.min.js");
		
		jsFilePathList.add("jqPlot/plugins/jqplot.barRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.categoryAxisRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.dateAxisRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.canvasTextRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.canvasAxisTickRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.pointLabels.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.pieRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.donutRenderer.min.js");
		jsFilePathList.add("jqPlot/plugins/jqplot.highlighter.min.js");

		return jsFilePathList;
	}

	public static final LinkedList<String> getCSSFilePathList(){
		LinkedList<String> cssFilePathList = new LinkedList<String>();
		
		// DataTables css
		cssFilePathList.add("DataTables-1.9.3/media/css/demo_table.css");
		
		// Adding jqPlot css
		cssFilePathList.add("jqPlot/jquery.jqplot.css");
		
		return cssFilePathList;
	}

	public static HTMLView getDOMElementToHTMLObjectClass(Element el, Table table)
	{
		HTMLView htmlrv = null;
		
		if(el.getNodeName().equals(XMLValues.CHART_LINE_CONTENT))
			htmlrv = new HTMLLineChartView(el, table);
		else if(el.getNodeName().equals(XMLValues.CHART_BAR_CONTENT))
			htmlrv = new HTMLBarChartView(el, table);
		else if(el.getNodeName().equals(XMLValues.CHART_PIE_CONTENT))
			htmlrv = new HTMLPieChartView(el, table);
		else if(el.getNodeName().equals(XMLValues.BR))
			htmlrv = new HTMLBrView(el, table);
		else if(el.getNodeName().equals(XMLValues.DIV))
			htmlrv = new HTMLDivView(el, table);
		else if(el.getNodeName().equals(XMLValues.TWO_COLS))
			htmlrv = new HTMLTwoColsView(el, table);
		else if(el.getNodeName().equals(XMLValues.TEXT_CONTENT))
			htmlrv = new HTMLTextContentView(el, table);
		else if(el.getNodeName().equals(XMLValues.TABLE_CONTENT))
			htmlrv = new HTMLTableView(el, table);
		
		return htmlrv;
	}
}
